﻿@{
    ViewBag.Title = "Company";
}

<h2>公司</h2>
<input id="ss" /> 
@*<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="clearSearch()">清除查询</a>  *@
<table id="dg"></table>
<script>
    $(document).ready(function () {
        loadGrid();
    });
    function clearSearch() {
        $('#dg').attr("Enabled", true);
    }
    var hideIndexs = new Array();
    $('#ss').searchbox({
        width: 200,
        searcher: function (value) {
             hideIndexs.length = 0;
             if (value != '') {
                 var rows = $('#dg').datagrid('getRows');

                 var cols = $('#dg').datagrid('options').columns[0];

                 for (var i = rows.length - 1; i >= 0; i--) {
                     var row = rows[i];
                     var isMatch = false;
                     for (var j = 0; j < cols.length; j++) {

                         var pValue = row[cols[j].field];
                         if (pValue.toString().indexOf(value) >= 0) {
                             isMatch = true;
                             break;
                         }
                     }
                     if (!isMatch)
                         hideIndexs.push(i);

                 }
             }
            $('#dg').datagrid('reload');
        },
        prompt: '请输入查询内容'
    });
    function endEdit() {
        var rows = $('#dg').datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            $('#dg').datagrid('endEdit', i);
        }
    }

    function loadGrid() {

        $('#dg').datagrid({
            url: '/Company/GetList',
            loadMsg: '数据加载中请稍后……',
          
            rownumbers: true,
            columns: [[
                { field: 'ID', title: '编号', width: 100, readonly: true },
                { field: 'Name', title: '名称', width: 100, editor: "validatebox" }
            ]],
            rowStyler: function (index, row) {
                if (hideIndexs.indexOf(index)>=0) { return 'background:red; display:none'; }
            },
            toolbar: [{
                text: "添加",
                iconCls: "icon-add",
                handler: function () {
                     $('#dg').datagrid('appendRow', {});
                    var rows =  $('#dg').datagrid('getRows');
                     $('#dg').datagrid('beginEdit', rows.length - 1);
                }
            }, {
                text: "编辑",
                iconCls: "icon-edit",
                handler: function () {
                    var row =  $('#dg').datagrid('getSelected');
                    if (row) {
                        var rowIndex =  $('#dg').datagrid('getRowIndex', row);
                         $('#dg').datagrid('beginEdit', rowIndex);
                    }
                }
            }, {
                text: "删除",
                iconCls: "icon-remove",
                handler: function () {
                    var row =  $('#dg').datagrid('getSelected');
                    if (row) {
                        var rowIndex =  $('#dg').datagrid('getRowIndex', row);
                         $('#dg').datagrid('deleteRow', rowIndex);
                    }
                }
            }, {
                text: "结束编辑",
                iconCls: "icon-cancel",
                handler: function () {
                    endEdit();
                }
            }, {
                text: "保存",
                iconCls: "icon-save",
                handler: function () {
                    endEdit();
                    if ( $('#dg').datagrid('getChanges').length) {
                        var inserted =  $('#dg').datagrid('getChanges', "inserted");
                        var deleted =  $('#dg').datagrid('getChanges', "deleted");
                        var updated =  $('#dg').datagrid('getChanges', "updated");

                        var effectRow = new Object();
                        if (inserted.length) {
                            effectRow["inserted"] = JSON.stringify(inserted);
                        }
                        if (deleted.length) {
                            effectRow["deleted"] = JSON.stringify(deleted);
                        }
                        if (updated.length) {
                            effectRow["updated"] = JSON.stringify(updated);
                        }

                        $.post("/Company/SaveList", effectRow, function (rsp) {
                            if (rsp=="Success") {
                                alert("提交成功！");
                                 $('#dg').datagrid('acceptChanges');
                            }
                        }, "JSON").error(function () {
                            alert("提交错误了！");
                        });
                    }
                }
            }]
        });

        $("#dg").datagrid('reload');
    }
</script>